package com.baixiaowen.xiaoaointerview.大厂笔试_白板面试篇.容器_集合_映射;

import java.util.*;

/**
 * 请用iterable实现一个随机序列产生器
 *
 * @author Baixiaowen
 */
public class RandomStringGenerator<T> implements Iterable {

    private final List<T> list;

    private RandomStringGenerator(List<T> list) {
        this.list = list;
    }

    public Iterator iterator() {

        return new Iterator<T>() {
            public boolean hasNext() {
                return true;
            }

            public T next() {
                return list.get((int) (list.size() * Math.random()));
            }
        };
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("List", "Tree", "Array");
        RandomStringGenerator gen = new RandomStringGenerator<String>(list);

        Iterator it = gen.iterator();
        for (int i = 0; i < 100; i++) {
            System.err.println(it.next());
        }

        final Class<Collection> collectionClass = Collection.class;

        ArrayList<String> arr = new ArrayList<String>();
        arr.add("123");
        Object[] objects = arr.toArray();
        String[] strings = arr.toArray(new String[0]);

    }
}
